﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Solution.Business.Component;
using Solution.Business.Entities;
using Solution.Data;

namespace Solution.UI.Web.Controllers.Admin
{
    public class AdminLoginController : Controller
    {
        //
        // GET: /AdminLogin/

        public ActionResult AdminLogin()
        {
            //User us = new User();
            return View();
        }

        [HttpPost]
        public ActionResult AdminLogin(User us, string submitButton)
        {
                switch(submitButton)
                {
                    case "Đăng Nhập" :
                        if (us.UserName != null && us.Password != null)
                        {
                            var list = Business.Component.UserComponent.User_GetByTop("5", "UserName ='" + us.UserName + "' and Password='" + us.Password + "' and Admin = 1", "");
                            if (list.Count > 0)
                            {
                                Session["Admin"] = list[0].UserName;
                                //FormsAuthentication.SetAuthCookie(us.UserName, false);
                                return RedirectToAction("Admin", "Admin");
                            }
                            else
                            {
                                ViewBag.mess = "Sai Tai Khoan Hoac Mat Khau";
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("", "Chưa nhập tên đăng nhập hoặc mật khẩu");
                        }
                break;
            }
        
            return View("AdminLogin", us);
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            Session["Admin"] = null;
            return RedirectToAction("Admin", "Admin");
        }

        public ActionResult Error()
        {
            return View("Error");
        }


    }
}
